package com.bootdo.common.geneseq;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.bootdo.common.geneseq.sequence.Sequence;

//初始化序列号生成器
public class DbSeqFactory {
	private final static Lock lock = new ReentrantLock();

	static Map<String, Sequence> sequenceMap = new ConcurrentHashMap<String, Sequence>();
	
	public static Sequence getSequence(String serviceCode) {
		
		Sequence sequence = sequenceMap.get(serviceCode);
		
		if(sequence == null) {
			lock.lock();
            try {
                if (sequence == null) {
                	//FIXME 根据业务名称创建序列号生成器
                	sequence = DbSeqBuilder.create().build(serviceCode);
        			sequenceMap.put(serviceCode, sequence);            
        		}
            } finally {
                lock.unlock();
            }
			
		}
		return sequence;
	}

}
